﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Craftsman.Core.Domain;
using Craftsman.Core.Infrastructure.Logging;
using Craftsman.Core.Runtime;
using Microsoft.AspNetCore.Mvc;
using Craftsman.Core.Web;
using Craftsman.xCoronavirus.Domain.Entities;
using Craftsman.Core.ObjectMapping;
using Craftsman.xCoronavirus.Domain;
using Craftsman.xCoronavirus.WebApi.Dto;
using Craftsman.xCoronavirus.Domain.Dto;

namespace Craftsman.xCoronavirus.WebApis.Controllers
{
    [Route("api/modify-requests")]
    [ApiController]
    public class ModifyRequestController : ControllerBase, IController
    {
        private ILogger _logger;
        private ISession _session;
        private IObjectMapper _objectMapper;
        public IModifyRequestService _modifyRequestlService { get; set; }
        public IHospitalService _hospitalService { get; set; }

        public ModifyRequestController(
            ILogger logger,
            ISession session,
            IObjectMapper objectMapper,
            IModifyRequestService modifyRequestlService,
            IHospitalService hospitalService
        )
        {
            _logger = logger;
            _session = session;
            _objectMapper = objectMapper;
            _modifyRequestlService = modifyRequestlService;
            _hospitalService = hospitalService;
        }


        #region modify_requests
        [HttpPost]
        public IActionResult CreateHospital([FromBody]ModifyRequest model)
        {
            _modifyRequestlService.CreateModifyRequest(model);
            return Created("api/hospitals", model);
        }

        // GET: api/modify_requests
        [HttpGet]
        public PagingDataRespone<ModifyRequestHospitalDTO> GetAllModifyRequests([FromQuery] string HospitalName, [FromQuery] string City, [FromQuery] ModifyRequestStatus[] Status )
        {
            var datas = _modifyRequestlService.GetModifyRequests(HospitalName, City, Status);

            return new PagingDataRespone<ModifyRequestHospitalDTO>() { Data = datas, Pagination = new Pagination() };
        }

        // GET api/modify_requests/{guid}
        [HttpGet("{id}")]
        public ModifyRequestDTO GetModifyRequest([FromRoute]string id)
        {
            var mrDto = new ModifyRequestDTO();
            var mr = _modifyRequestlService.GetModifyRequest(id);
            var hospital = _hospitalService.GetHospital(mr.HospitalId);

            foreach (var mrContactItem in mr.Contacts)
            {
                mrDto.Contacts.Add(new ModifyRequestContactDTO()
                {
                    New = mrContactItem,
                    Old = hospital.Contacts.FirstOrDefault(c=>c.Id == mrContactItem.ContactId)
                });
            }
            foreach (var mrMaterialDemandsItem in mr.MaterialDemands)
            {
                mrDto.MaterialDemands.Add(new ModifyRequestMaterialDemandDTO()
                {
                    New = mrMaterialDemandsItem,
                    Old = hospital.MaterialDemands.FirstOrDefault(c => c.Id == mrMaterialDemandsItem.DemandId)
                });
            }
            mr.Contacts = null;
            mr.MaterialDemands = null;
            hospital.Contacts = null;
            hospital.MaterialDemands = null;
            mrDto.ModifyRequest = mr;
            mrDto.HospitalInfo = hospital;
            return mrDto;
        }

        // GET api/modify_requests/{guid}/pass
        [HttpPost("{id}/pass")]
        public IActionResult PassModifyRequest([FromRoute]string id)
        {
            _modifyRequestlService.PassModifyRequest(id);
            return NoContent();
        }

        [HttpPost("{id}/invalid")]
        public IActionResult InvalidModifyRequest([FromRoute]string id)
        {
            _modifyRequestlService.InvalidModifyRequest(id);
            return NoContent();
        }

        #endregion modify_requests
    }
}
